package com.long1008.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.long1008.common.DTreeUtils;
import com.long1008.mapper.AllSalaryMapper;
import com.long1008.mapper.EmpMapper;
import com.long1008.mapper.PositionMapper;
import com.long1008.pojo.AllSalary;
import com.long1008.pojo.DModel.TurnoverModelDto;
import com.long1008.pojo.DModel.TurnoverResult;
import com.long1008.pojo.Employee;
import com.long1008.pojo.Position;
import com.long1008.pojo.Salary;
import com.long1008.service.AllSalaryService;
import com.long1008.service.DTreeService;
import org.dmg.pmml.FieldName;
import org.jpmml.evaluator.Evaluator;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DTreeServiceImpl extends ServiceImpl<EmpMapper,Employee> implements DTreeService {

    @Resource
    PositionMapper positionMapper;

    @Resource
    AllSalaryService allSalaryService;

    @Resource
    AllSalaryMapper allSalaryMapper;

    @Resource
    DTreeUtils dTreeUtils;

    @Override
    public List<Employee> selectEmpByKeywordsToShow(String keywords){
        String temp = "all";
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();

        if(!keywords.equals(temp) && keywords != ""){
            wrapper.like("name",keywords).select("id","name","gender","phone","email","address");
        }else if(keywords.equals(temp)){
            wrapper.select("id","name","gender","phone","email","address").last("limit 8");
        }

        List<Employee> employees = baseMapper.selectList(wrapper);

        return employees;
    }

    @Override
    public Map<String,Object> selectOneEmpDetail(Long EmpId){
        Map<String,Object> emp = new HashMap<>();

        Employee empTemp = baseMapper.getEmpById(EmpId);

        //获取工作年数
        Calendar calendar = Calendar.getInstance();
        //获取当前年份
        int nowDate = calendar.get(Calendar.YEAR);
        //获取入职时间
        calendar.setTime(empTemp.getBeginDate());
        int beginDate = calendar.get(Calendar.YEAR);
        int timeSpendCompany = nowDate - beginDate;

        //获取职位
        Long posId = empTemp.getPosId();
        Position position = positionMapper.selectById(posId);
        String posName = position.getName();

        //获取工资
        LambdaQueryWrapper<AllSalary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AllSalary::getEid,EmpId);
        AllSalary salary = allSalaryService.getOne(lambdaQueryWrapper);

        //获取平均工资
        QueryWrapper<AllSalary> allSalaryQueryWrapper = new QueryWrapper<>();
        allSalaryQueryWrapper.select("AVG(monthSalary) as avgSalary");
        AllSalary salary2 = allSalaryMapper.selectOne(allSalaryQueryWrapper);

        emp.put("id",EmpId);
        emp.put("name",empTemp.getName());
        emp.put("timeSpendCompany",timeSpendCompany);
        emp.put("position",posName);
        emp.put("salary",salary.getMonthSalary());
        emp.put("avgSalary",salary2.getAvgSalary());

        return emp;

    }

    @Override
    public Integer turnoverProjections(TurnoverModelDto dto){
        Integer result;

        //模型文件位置
        String pmmlFileName = "src/main/resources/model/DTC_Fit_To_Pmml.pmml";

        //加载文件
        Evaluator modelEvaluator = dTreeUtils.LoadPmml(pmmlFileName);
        Map<FieldName, Object> arguments = new HashMap<>();

        //输入参数
        arguments.put(FieldName.create("satisfaction_level"), dto.getSatisfactionLevel());
        arguments.put(FieldName.create("last_evaluation"), dto.getLastEvaluation());
        arguments.put(FieldName.create("number_project"), dto.getNumberProject());
        arguments.put(FieldName.create("average_montly_hours"), dto.getAverageMontlyHours());
        arguments.put(FieldName.create("time_spend_company"), dto.getTimeSpendCompany());
        arguments.put(FieldName.create("Work_accident"), dto.getWorkAccident());
        arguments.put(FieldName.create("promotion_last_5years"), dto.getPromotionLast5years());
        arguments.put(FieldName.create("salary"), dto.getSalaryLevel());
        Integer positionType = dto.getPositionType();
        arguments.put(FieldName.create("IT"), 0);
        arguments.put(FieldName.create("RandD"), 0);
        arguments.put(FieldName.create("accounting"), 0);
        arguments.put(FieldName.create("hr"), 0);
        arguments.put(FieldName.create("management"), 0);
        arguments.put(FieldName.create("marketing"), 0);
        arguments.put(FieldName.create("product_mng"), 0);
        arguments.put(FieldName.create("sales"), 1);
        arguments.put(FieldName.create("support"), 0);
        arguments.put(FieldName.create("technical"), 0);
        if(positionType == 1){
            arguments.remove("IT");
            arguments.put(FieldName.create("IT"),1);
        }else if (positionType == 2){
            arguments.remove("RandD");
            arguments.put(FieldName.create("RandD"),1);
        }else if (positionType == 3){
            arguments.remove("accounting");
            arguments.put(FieldName.create("accounting"),1);
        }else if (positionType == 4){
            arguments.remove("hr");
            arguments.put(FieldName.create("hr"),1);
        }else if (positionType == 5){
            arguments.remove("management");
            arguments.put(FieldName.create("management"),1);
        }else if (positionType == 6){
            arguments.remove("marketing");
            arguments.put(FieldName.create("marketing"),1);
        }else if (positionType == 7){
            arguments.remove("product_mng");
            arguments.put(FieldName.create("product_mng"),1);
        }else if (positionType == 8){
            arguments.remove("sales");
            arguments.put(FieldName.create("sales"),1);
        }else if (positionType == 9){
            arguments.remove("support");
            arguments.put(FieldName.create("support"),1);
        }else if (positionType == 10){
            arguments.remove("technical");
            arguments.put(FieldName.create("technical"),1);
        }

        Object targetValue = dTreeUtils.predict(modelEvaluator,arguments);

        TurnoverResult turnoverResult = BeanUtil.copyProperties(targetValue, TurnoverResult.class);
        return turnoverResult.getResult();
    }
}
